$package('Bdth.Department');
Bdth.Department = function () {
	var _box = null;
	//var username = $("#username").val();
	var _this = {
		config: {
			event: {
				edit: function () {
					var record = Utils.getCheckedRows();
					if (Utils.checkSelectOne(record)) {
						Bdth.progress();
						var data = {};
						var idKey = 'id'; // 主键名称
						data[idKey] = (record[0][idKey]);
						Bdth.getById('getId', data, function (result) {
							Bdth.closeProgress();
							$("#edit-win").dialog({
								href: path + '/view/department/form.html',
								width: 600,
								height: 365,
								modal: true,
								resizable: true,
								/*collapsible: true,
								 maximizable: true,*/
								onLoad: function () {
									$("#editForm").form('load', result.data);
									$("#edit-department").removeClass('depInputClass');
								},
								title: '编辑',
								buttons: [{
									text: '保存',
									iconCls: 'fa fa-save',
									btnCls: 'topjui-btn',
									handler: _box.handler.save
								}],
							});

						});
					}
				},
			},
			dataGrid: {
				title: '分组列表',
				url: 'dataList',
				//queryParams: { "username": username },
				columns: [[
					{field: 'id', checkbox: true, title: 'id'},
					{field: 'depName', title: '分组名称', width: 120, sortable: false},
					{field: 'createTime', title: '创建时间', width: 120, sortable: false},
					{field: 'parentDepName', title: '上级分组', width: 120, sortable: false},
				]],
				toolbar: [
					{id: 'btnadd', text: '添加', btnType: 'add'},
					{id: 'btnedit', text: '修改', btnType: 'edit'},
					{id: 'btndelete', text: '删除', btnType: 'remove'}
				]
			}, action: {
				add_editForm: '/view/department/form.html',
				remove: 'fakeDelete'
			},
			wid_height: {
				width: 600,
				height: 300
			},
		},
		init: function () {
			_box = new DataGrid(_this.config);
			_box.init();
		}
	}
	return _this;
}();

$(function () {
	Bdth.Department.init();
});

// Grid 工具类
var Utils = {
	getCheckedRows: function () {
		return $('#data-list').datagrid('getChecked');
	},
	checkSelect: function (rows) {// 检查grid是否有勾选的行, 有返回 true,没有返回true
		var records = rows;
		if (records && records.length > 0) {
			return true;
		}
		Bdth.alert('警告', '未选中记录.', 'messager-warning');
		return false;

	},
	checkSelectOne: function (rows) {// 检查grid是否只勾选了一行,是返回 true,否返回true
		var records = rows;
		if (!Utils.checkSelect(records)) {
			return false;
		}
		if (records.length == 1) {
			return true;
		}
		Bdth.alert('警告', '只能选择一行记录.', 'messager-error');
		return false;
	}
}